package com.ptr.ptrsystem.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ptr.controladores.conexionsqlserver;
import com.ptr.ptrsystem.dao.Lista;
import com.ptr.ptrsystem.logica.*;
import java.sql.*;

/**
 *
 * @author WALDIR
 */
@WebServlet(name = "MantenimientoDefuncionServlet", urlPatterns = {"/MantenimientoDefuncion"})
public class MantenimientoDefuncionServlet extends HttpServlet {

    public MantenimientoDefuncionServlet() {
    }
    String COD_PERSONA;
    String NRO_ACTA_CDEF;
    String FECH_REG_DEF;
    String NRO_LIBRO;
    //
    String dep;
    String prov;
    String dist;
    String otro;
    String IDUBIGEO;
    //
    String COD_LUG_OCC;
    String FECH_DEF;
    //
    String hor;
    String min;
    String seg;
    String HOR_DEF;
    //
    String DNI_M;
    String DNI_P;
    String DNI_DECL;
    String DNI_PER_REG;
    String COD_ESTADO;
    String COD_T_DOC;
    String OBS;
    String RUT_ACT_DEF;
    String RUT_ACT_DEF2;
    conexionsqlserver con = new conexionsqlserver();
    Connection cn = null;
    Statement st = null;
    ResultSet rs = null;
    String QUERY = "";

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        COD_PERSONA = request.getParameter("codpersona").trim();
        NRO_ACTA_CDEF = request.getParameter("NACDEF").trim();
        FECH_REG_DEF = request.getParameter("FechRegDef").trim();

        NRO_LIBRO = request.getParameter("NroLibro").trim();
        //ubigeo
        dep = request.getParameter("dep").trim();
        prov = request.getParameter("prov").trim();
        dist = request.getParameter("dist").trim();
        otro = request.getParameter("otro").trim();
        IDUBIGEO = dep + prov + dist + otro;
        //
        COD_LUG_OCC = request.getParameter("CodLugOcurr").trim();
        FECH_DEF = request.getParameter("FecDef").trim();
        //
        hor = request.getParameter("hor");
        min = request.getParameter("min");
        seg = request.getParameter("seg");
        HOR_DEF = hor + ":" + min + ":" + seg;
        //
        DNI_M = request.getParameter("DNIMadre").trim();
        DNI_P = request.getParameter("DNIPadre").trim();
        DNI_DECL = request.getParameter("DNIDecl").trim();
        DNI_PER_REG = request.getParameter("DNIPerReg").trim();
        COD_ESTADO = request.getParameter("CodEst");
        COD_T_DOC = request.getParameter("CodTDoc");
        OBS = request.getParameter("Obs");
        RUT_ACT_DEF = request.getParameter("RutActDef");
        RUT_ACT_DEF2 = request.getParameter("RutActDef2");
        String BTN = request.getParameter("btn1");
        if (BTN.equals("guardaDef")) {
            GuardarDefuncion(request, response);
        }

        if (BTN.equals("mostrarMADRE")) {
            NombreMadre(request, response);
        }
        if (BTN.equals("mostrarPADRE")) {
            NombrePadre(request, response);
        }
        if (BTN.equals("mostrarDECLARANTE")) {
            NombreDeclarante(request, response);
        }
        if (BTN.equals("mostrarPERSONAREG")) {
            NombrePerReg(request, response);
        }
    }

    protected void GuardarDefuncion(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        LogicaDefuncion Defun = new LogicaDefuncion();
        boolean DF = Defun.insertarDefuncion(COD_PERSONA, NRO_ACTA_CDEF, NRO_LIBRO, FECH_REG_DEF, IDUBIGEO, COD_LUG_OCC, FECH_DEF, HOR_DEF, DNI_M, DNI_P, DNI_DECL, DNI_PER_REG, COD_ESTADO, COD_T_DOC, OBS, RUT_ACT_DEF,RUT_ACT_DEF2);
        if (DF) {
            request.setAttribute("msj", "2");
            request.getRequestDispatcher("/jsp/NDefuncion.jsp").forward(request, response);
        } else {
            request.setAttribute("msj", "1");
            request.getRequestDispatcher("/jsp/NDefuncion.jsp").forward(request, response);
        }
    }

    protected void NombreMadre(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println(Lista.getNombrePersona(DNI_M));
    }

    protected void NombrePadre(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println(Lista.getNombrePersona(DNI_P));
    }

    protected void NombreDeclarante(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println(Lista.getNombrePersona(DNI_DECL));
    }

    protected void NombrePerReg(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println(Lista.getNombrePersona(DNI_PER_REG));
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
